<?php

namespace App\Exports;


use App\Models\ArchiveManageVolume;
use App\Models\LedgerEquipment;
use App\Models\LedgerTender;
use App\Models\Project;
use App\Models\Storeroom;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithCustomStartCell;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithTitle;

class LedgerEquipmentExport implements FromCollection, WithColumnWidths, WithStyles, WithCustomStartCell, WithTitle
{
    use Exportable;

    protected $totalRows;
    protected $projectId;
    protected $type;
    protected $keyword;
    protected $selectIds;
    protected $sheetName;
    protected $sheetTitle;

    public function __construct($projectId, $keyword, $selectIds)
    {
        $projectName = Project::query()->where('id', $projectId)->value('title');

        $this->projectId  = $projectId;
        $this->keyword    = $keyword;
        $this->selectIds  = $selectIds;
        $this->sheetName  = '物资设备台账';
        $this->sheetTitle = $projectName . $this->sheetName;
    }

    public function collection()
    {
        $thisQuery = LedgerEquipment::query()
            ->where('project_id', $this->projectId);
        if ($this->selectIds) {
            $thisQuery->whereIn('id', to_array($this->selectIds));
        }
        if ($this->keyword) {
            $likeKeyword = $this->keyword;
            $thisQuery->where('file_number', 'like', $likeKeyword)->orWhere('content', 'like', $likeKeyword);
        }
        $list = $thisQuery->orderBy('serial_no')->get();
        if ($list->isEmpty()) return [];
        $list   = $list->toArray();
        $r_list = [];
        foreach ($list as $val) {
            $tmp                     = [];
            $tmp['serial_no']        = $val['serial_no'];
            $tmp['name']             = $val['name'];
            $tmp['model_num']        = $val['model_num'];
            $tmp['remark']           = $val['remark'];
            $tmp['prepare_person']   = $val['prepare_person'];
            $tmp['reviewer']         = $val['reviewer'];
            $tmp['record_date']      = $val['record_date'];
            $r_list[]                = $tmp;
        }
        $this->totalRows = count($r_list) + 2;

        return collect($r_list);
    }

    /**
     * 从哪个单元格开始渲染数据，数据集合如 $list
     * @return string
     */
    public function startCell(): string
    {
        return 'A3';
    }

    /**
     * 设置列宽
     * @return array
     */
    public function columnWidths(): array
    {
        return [
            'A' => 15, 'B' => 30, 'C' => 20, 'D' => 15, 'E' => 15, 'F' => 15, 'G' => 15,
        ];
    }

    /**
     * 样式设置
     * @param Worksheet $sheet
     * @throws PhpSpreadsheetException
     */
    public function styles(Worksheet $sheet)
    {
        $sheet->getDefaultRowDimension()->setRowHeight(30); //设置行高(全部行高)
        $sheet->getStyle('A1:G' . $this->totalRows)->getFont()->setName('宋体'); //宋体
        $sheet->getStyle('A1:G' . $this->totalRows)->getFont()->setSize(12); //大小
        $sheet->getStyle('A1:G' . $this->totalRows)->getAlignment()->setWrapText(true); //自动换行

        $sheet->mergeCells('A1:G1');
        $sheet->setCellValue('A1', $this->sheetTitle);

        /**
         * 属性名设置
         */
        $sheet->setCellValue('A2', '序号');
        $sheet->setCellValue('B2', '物资设备名称');
        $sheet->setCellValue('C2', '物资设备型号');
        $sheet->setCellValue('D2', '备注');
        $sheet->setCellValue('E2', '编制人');
        $sheet->setCellValue('F2', '审核人');
        $sheet->setCellValue('G2', '日期');

        /**
         * 顶部案卷目录样式设置
         */
        $sheet->getStyle('A1:G' . $this->totalRows)->getAlignment()->setVertical('center');//垂直居中
        $sheet->getStyle('A1:G' . $this->totalRows)->getAlignment()->setHorizontal('center');//水平居左

        /**
         * 设置边框
         */
        $styleArray = [
            'borders' => [
                'allBorders' => [
                    'borderStyle' => Border::BORDER_THIN //细边框
                ]
            ]
        ];
        $sheet->getStyle('A1:G' . $this->totalRows)->applyFromArray($styleArray);
    }

    /**
     * sheet名称
     * @return string
     */
    public function title(): string
    {
        return $this->sheetName;
    }


}
